<?php

function diffBetweenTwoDays ($day1, $day2)
{
    $second1 = strtotime($day1);
    $second2 = strtotime($day2);

    if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
    }
    return ($second1 - $second2) / 86400;
}

function generate_code($length = 6) {
    return rand(pow(10,($length-1)), pow(10,$length)-1);
}

function diff_day($birthday){
    $date2 = $birthday;
    $date1 = date('Y-m-d');
    if(strtotime($date1)>strtotime($date2)){
        $tmp=$date2;
        $date2=$date1;
        $date1=$tmp;
    }
    list($Y1,$m1,$d1)=explode('-',$date1);
    list($Y2,$m2,$d2)=explode('-',$date2);
    $Y=$Y2-$Y1;
    $m=$m2-$m1;
    $d=$d2-$d1;
    if($d<0){
        $d+=(int)date('t',strtotime("-1 month $date2"));
        $m--;
    }
    if($m<0){
        $m+=12;
        $Y--;
    }

    return array('year'=>$Y,'month'=>$m, 'd' => $d);
}


function birthday($birthday, $is_round = 1){
    $arr = diff_day($birthday);
    if ($is_round) {
        if ($arr['month'] >= 6) {
            return ++$arr['year'];
        } else {
            return $arr['year'];
        }
    }
    return $arr;

    $strto_birthday = strtotime($birthday);
    $byear=date('Y',$strto_birthday);
    $bmonth=date('m',$strto_birthday);
    $bday=date('d',$strto_birthday);

    $tyear=date('Y');
    $tmonth=date('m');
    $tday=date('d');

    $age=$tyear-$byear;

    if($bmonth>$tmonth || ($bmonth==$tmonth && $bday>$tday)){
        $age--;
    }
    return $age;
}


function create_password($length = 6,$type = 'alpha-number')
{
    $code_arr = array(
        'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        'number'=> '0123456789',
        'sign'  => '#$%@*-_',
    );

    $type_arr = explode('-',$type);

    foreach($type_arr as $t){
        if( ! array_key_exists($t,$code_arr)){
            trigger_error("Can not generate type ($t) code");
        }
    }

    $chars = '';

    foreach($type_arr as $t){
        $chars .= $code_arr[$t];
    }
    $chars = str_shuffle($chars);
    $number = $length > strlen($chars) - 1 ? strlen($chars) - 1:$length;
    return substr($chars,0,$number);
}

function json_encode_cn($arr = []) {
    return json_encode($arr, JSON_UNESCAPED_UNICODE);
}

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

function format_yuan($fen) {
    return number_format($fen / 100, 2);
}

function toCamelCase($str)
{
    $array = explode('_', $str);
    $result = $array[0];
    $len=count($array);
    if($len>1)
    {
        for($i=1;$i<$len;$i++)
        {
            $result.= ucfirst($array[$i]);
        }
    }
    return $result;
}

function per_page($per_page = 20) {
    return \request('per_page', $per_page);
}

function create_order_no($prefix = '') {
    return $prefix . date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}


function time2string($second){
    $day = floor($second/(3600*24));
    $second = $second%(3600*24);//除去整天之后剩余的时间
    $hour = floor($second/3600);
    $second = $second%3600;//除去整小时之后剩余的时间
    $minute = floor($second/60);
    $second = $second%60;//除去整分钟之后剩余的时间
    //返回字符串
    return $day.'天'.$hour.'小时'.$minute.'分';
}